import { ref } from "vue";
import { ElMessage } from "element-plus";
import { getRoleAPI, deleteRoleAPI } from "@/api/user.ts";

export default function useRole() {
  const roleList = ref([])
  const dialog = ref()

  // 获取角色列表
  const getRoleList = async () => {
    const res = await getRoleAPI()
    roleList.value = res.data
  }

  // 添加角色
  const addRole = () => {
    dialog.value.init({}, 0)
  }

  // 编辑角色
  const EditRole = (row: any) => {
    dialog.value.init(row, 1)
  }

  // 删除角色
  const deleteRole = async (id: string) => {
    const res: any = await deleteRoleAPI(id)
    ElMessage({
      type: res.code === 0 ? 'success' : 'error',
      message: res.message
    })
    await getRoleList()
  }

  return {
    roleList,
    dialog,
    getRoleList,
    addRole,
    EditRole,
    deleteRole
  }
}
